#! /usr/bin/python
# coding=utf-8

import logging
from telnetlib import IP

from scapy.layers.inet import TCP

logging.getLogger("scapy.runtime").setLevel(logging.INFO)
from scapy.all import *

def tcpscan():
    dst_ip = "123.57.248.123"
    src_port = RandShort()
    print src_port
    dst_port=80

    tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"),timeout=10)
    if(str(type(tcp_connect_scan_resp))==""):
        print "Closed"
    elif(tcp_connect_scan_resp.haslayer(TCP)):
        if(tcp_connect_scan_resp.getlayer(TCP).flags == 0x12):
            send_rst = sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="AR"),timeout=10)
            print "Open"
    elif (tcp_connect_scan_resp.getlayer(TCP).flags == 0x14):
        print "Closed"

if __name__ == "__main__":
    print "开始TCP扫描"
    logging.info("开始TCP扫描")
    tcpscan()